using System.Reflection;
using Elsa.Studio.Contracts;
using Elsa.Studio.Models;

namespace Elsa.Studio.Services;

/// <summary>
/// Provides client information by extracting metadata from the executing assembly.
/// </summary>
public class AssemblyClientInformationProvider : IClientInformationProvider
{
    /// <inheritdoc />
    public ValueTask<ClientInformation> GetInfoAsync(CancellationToken cancellationToken = default)
    {
        var assembly = Assembly.GetExecutingAssembly();
        var versionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
        var version = RemoveAutoGeneratedPostfix(versionAttribute);

        return new(new ClientInformation(version));
    }

    private static string RemoveAutoGeneratedPostfix(AssemblyInformationalVersionAttribute? versionAttribute)
    {
        return versionAttribute?.InformationalVersion.Split("+")[0]!;
    }
}